﻿using PDFDesign.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace PDFDesign.Repository
{
    public class ReportStyleRepository : BaseRepository<ReportStyles>
    {
        private static ReportStyleRepository reportStyleRepository;
        private static readonly object locker = new object();
        private ReportStyleRepository() { }
        public static ReportStyleRepository GetInstance()
        {
            if (reportStyleRepository == null)
            {
                lock (locker)
                {
                    if (reportStyleRepository == null)
                    {
                        reportStyleRepository = new ReportStyleRepository();
                    }
                }
            }
            return reportStyleRepository;
        }

        /// <summary>
        /// 获取样式列表
        /// </summary>
        /// <param name="templateName"></param>
        /// <returns></returns>
        public List<ReportStyles> GetReportStyleList(string templateName = "")
        {
            var sql = "select * from reportStyles where styleType=1 and styleFomatType=1 ";
            if (!string.IsNullOrWhiteSpace(templateName))
            {
                sql += "and templateName like @templateName";
            }
            var results = Query(sql, new { templateName = $"%{templateName}%" }).ToList();
            return results;
        }

        /// <summary>
        /// 判断模板名和样式名是否存在
        /// </summary>
        /// <param name="templateName"></param>
        /// <param name="styleName"></param>
        /// <returns></returns>
        public bool IsExistReportStyle(string templateName, string styleName)
        {
            var sql = "select id from reportStyles where styleType=1 and styleFomatType=1 and templateName=@templateName and styleName=@styleName ";
            var count = Query(sql, new { templateName, styleName }).Count();
            return count > 0;
        }
    }
}
